import 'package:badges/badges.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shark_im/entity/message/base_message_entity.dart';
import 'package:flutter_shark_im/entity/message/user_list_response_entity.dart';
import 'chat_page.dart';

class Home extends StatefulWidget {

  final EventBus eventBus;

  Home({this.eventBus});

  @override
  State<StatefulWidget> createState() {
    return new HomeState();
  }

}

class HomeState extends State<Home> {

  int _currentPage = 0;

  Color _bottomColor = Colors.blue;

  List<User> _users = [];

  @override
  void initState() {
    widget.eventBus.on<UserListResponseEntity>().listen((resp) {
      _users = resp.users;
    });
    widget.eventBus.fire(GetUserListEntity().toJson());
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'zhuye',
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text(_currentPage == 0 ? '消息' : '联系人'),
        ),
        body: _buildListView(),
        bottomNavigationBar: BottomNavigationBar(
          items: [
            BottomNavigationBarItem(
              icon: Icon(Icons.message, color: Colors.black,),
              activeIcon: Icon(Icons.message, color: _bottomColor,),
              title: Text('消息'),
            ),
            BottomNavigationBarItem(
                icon: Icon(Icons.perm_contact_calendar, color: Colors.black,),
                activeIcon: Icon(Icons.perm_contact_calendar, color: _bottomColor,),
                title: Text('联系人'),
            )
          ],
          selectedItemColor: _bottomColor,
          currentIndex: _currentPage,
          onTap: (index) {
            setState(() {
              _currentPage = index;
              if (_currentPage == 1) {
                widget.eventBus.fire(GetUserListEntity().toJson());
              }
            });
          },
        ),
      )
    );
  }

  Widget _buildListView() {
    return ListView.builder(
      itemCount: _currentPage == 0 ? 10 : _users.length,
      itemBuilder: (_, index) {
        return AnimatedSwitcher(
          transitionBuilder: (child, anim) {
            return ScaleTransition(child: child, scale: anim);
          },
          duration: Duration(milliseconds: 300),
          child: Container(
              key: ValueKey<int>(_currentPage),
              child: _currentPage == 0 ? _message(index) : _concat(index)),
        );
      });
  }

  ListTile _message(index) {
    return new ListTile(
      onTap: (){
        if (index.isOdd) {
          widget.eventBus.fire('String');
        } else {
          widget.eventBus.fire(index);
        }
      },
      leading: Badge(
          badgeContent: Text('3',
            style: TextStyle(
                color: Colors.white
            ),),
          child: new ClipRRect(
            borderRadius: BorderRadius.circular(6.0),
            child: new Image.asset('asserts/1.png',width: 45.0,height: 45.0,fit: BoxFit.cover,),
          )
      ),
      title: new Text("白骨精"),
      subtitle: new Text('唐僧走到哪里了?'),
      trailing: new Text("9:00"),
    );
  }

  ListTile _concat(int index) {
    final user = _users[index];
    return new ListTile(
      key: ValueKey<String>(user.id),
      title: new Text(user.name),
      leading:new Image.asset('asserts/${user.avatar}.png',width: 35.0,height: 35.0,),
      onTap: () {
        print(user.id);
        Navigator.of(context).push(MaterialPageRoute(
          builder: (context) => Chat(widget.eventBus, user)
        ));
      },
    );
  }

}